package models

import (
	"gorm.io/gorm"
	"im/global"
)

type Video struct {
	Id        int
	Title     string
	VideoUrl  string
	VideoTime string
	VideoImg  string
	VideoSort string
	SubTitle  string
}

func (v *Video) List(title string) (video []Video, res *gorm.DB, err error) {
	db := global.Db.Table("video")

	if title != "" {
		db.Where("title like ?", "%"+title+"%")
	}

	res = db.Find(&video)

	return video, res, nil
}

func (v *Video) Sort(video_sort int) (video []Video, err error) {

	db := global.Db.Table("video")

	if video_sort != 0 {
		switch video_sort {
		case 1:
			db.Where("video_sort = ?", video_sort)
		case 2:
			db.Where("video_sort = ?", video_sort)
		case 3:
			db.Where("video_sort = ?", video_sort)
		case 4:
			db.Where("video_sort = ?", video_sort)

		}
	}

	db.Find(&video)

	return video, nil

}

func (v *Video) Search(id int) (video Video, err error) {

	db := global.Db.Table("video")

	if id != 0 {

		db.Where("id = ?", id)
	}

	db.First(&video)

	return video, nil
}
